<?php
/**
 * 飞信机器人--功能插件--英文词典
 * 功能：实现英文词典功能
 * 短指令： #fy 词
 * 数据表：无
 * 作者： Kelvin
 * 联系： msn:shichangguo@msn.com, qq:191848423
*/
class Dict extends CommonPlugin
{
	var $UUID='379bc60d-b57a-4b0c-be6e-e31a5cd5c45b';

	public function Dict(&$session, $pid, $extraPara="")
	{
		parent::__construct($session, $pid, $extraPara);
	}
		
	function getTips()
	{
		return "请输入需要翻译的中英文单词。".$this->getFooter();
	}
	
	function shortcut($input, &$done)
	{
		if( strcasecmp(substr($input,0,4),'#fy ') )return false;
		$content = trim(substr($input, 4));
		if( strlen($content) == 0 )return false;
		
		$done = true;
		return $this->handle($content,true);
	}
	function handle($input, $shortcut = false)
	{
		if( parent::handleInput($input, $shortcut)) return false;

		$content = $input;
		$content = urlencode($input);
		$svcUrl = "http://www.dict.cn/{content}.htm";
		$svcUrl = str_replace('{content}', $content, $svcUrl);
		$content = file_get_contents($svcUrl);
		
		$content = iconv('gbk', 'utf-8', $content);
		$start = "<div class=\"mut_jies\">";
		$end  = "</div>";
		
		$posStart = strpos($content, $start)+strlen($start);
		$posEnd = strpos($content, $end, $posStart);
		
		$dict = substr($content, $posStart, $posEnd-$posStart);
		$dict = " ".str_replace("<br />", "\n", $dict);

		$out = "<font color=\"red\"><b>翻译结果：</b></font>\n".trim($dict)."\n\n";
		
		$start2 = "<h3>例句与用法:</h3>";
		$end2 = "</ol></div>";
		$posStart2 = strpos($content, $start2,$posEnd);
		
		if(!($posStart2 === false) )
		{
			$posEnd2 = strpos($content, $end2, $posStart2);
			
			$sub = substr($content,$posStart2+strlen($start2), $posEnd2-$posStart2-strlen($start2)+1);
			$sub = strip_tags($sub);
			
			$out.="<font color=\"red\"><b>例句与用法:</b></font>".$sub;
		}
		
		
		if( strpos($out, "您要查找的是不是:")>0 )
			$out = "<Font Face='楷体' Color='-16744320' Size='10.5'>对不起，系统无法为您翻译该单词：$userInput</font>";
			
		return $out.$this->getFooter('',$shortcut);
	}	
	
}

?>
